﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Txt2Shp4Sharp.geojson;

namespace Txt2Shp4Sharp.landm
{
    public class Block
    {
        /*
	    4,0.012,1,陈柏怀,面,H49G031010,农村宅基地,1,@
	    J1,1,3403393.438,36559307.520
	    J2,1,3403392.408,36559319.280
	    J3,1,3403382.235,36559318.710
	    J4,1,3403383.570,36559306.670
	    J1,1,3403393.438,36559307.520
	    */
        //界址点数，如4
        private int jzds = 0;
        //地块面积,如0.012
        private double dkmj = 0.0;
        //地块编号,如1
        private String dkbh = "";
        //地块名称,如陈柏怀
        private String dkmc = "";
        //记录图形属性,如面
        private String jltxsx = "";
        //图幅号,如H49G031010
        private String tfh = "";
        //地块用途,如农村宅基地
        private String dkyt = "";
        //地类编码,如1
        private String dlbh = "";

        //地块的ring环的集合
        private List<Ring> dkRings=null;
        //地块属性集合
        private Dictionary<String, Object> dkAttrs = null;


        //构造函数
        public Block()
        {
            this.dkRings = new List<Ring>();
            this.dkAttrs = new Dictionary<String, Object>();
        }

        #region  属性
        /// <summary>
        /// 界址点数
        /// </summary>
        public int Jzds{
            get{
                return jzds;
            }
            set {
                this.jzds = value;
            }
        }

        /// <summary>
        /// 地块面积
        /// </summary>
        public double Dkmj
        {
            get
            {
                return dkmj;
            }
            set {
                this.dkmj = value;
            }
        }
       
        /// <summary>
        /// 地块编号
        /// </summary>
        public String Dkbh{
            get
            {
                return dkbh;
            }
            set {
                this.dkbh = value;
            }
        }
       
        /// <summary>
        /// 地块名称
        /// </summary>
        public String Dkmc{
            get{
                return dkmc;
            }
            set {
                this.dkmc = value;
            }
        }
       
        /// <summary>
        /// 记录图形参数
        /// </summary>
        public String Jltxsx{
            get{
                return jltxsx;
            }
            set { 
                this.jltxsx = value;
            }
        }

        /// <summary>
        /// 图幅号
        /// </summary>
        public String Tfh
        {
            get
            {
                return tfh;
            }
            set {
                this.tfh = value;
            }
        }

        /// <summary>
        /// 地块用途
        /// </summary>
        public String Dkyt
        {
            get
            {
                return dkyt;
            }
            set {
                this.dkyt = value;
            }
        }
       
        /// <summary>
        /// 地类编号
        /// </summary>
        public String Dlbh{
             get{
                  return dlbh;
            }
            set {
                this.dlbh = value;
            }
        }

        //属性
        public List<Ring> DKRings{
            get
            {
                return this.dkRings;
            }
        }

        //所有属性集合
        public Dictionary<String, Object> DKAttrs
        {
            get{
                return dkAttrs;
            }
            set {
                this.dkAttrs = value;
            }
        }

        #endregion


        /**
         * 生成当前地块图形的GeoJSON串
         * 
         * @return
         */
        public String toJsonStr()
        {
            GeoJsonFeature fea = new GeoJsonFeature();
            fea.FeaType ="Feature";
            try
            {
                int ringCount = this.DKRings.Count;
                //获取某个地块block的界址点数
                int jzdCount = 0;
                for (int ii = 0; ii < ringCount; ii++)
                {
                    Ring ring = this.DKRings[ii];
                    int ringJzdCount = ring.JzdPoints.Count;

                    jzdCount += ringJzdCount;
                }

                //属性信息
                Dictionary<String, Object> properties = new Dictionary<String, Object>();

                String dkmj = String.Format("%.4f", this.Dkmj);
                String dkbh = this.Dkbh;
                String dkmc = this.Dkmc;
                String jltxsx = this.Jltxsx;
                String tfh = this.Tfh;
                String dkyt = this.Dkyt;
                properties.Add("dkbh", dkbh);
                properties.Add("dkmc", dkmc);
                properties.Add("jltxsx", jltxsx);
                properties.Add("tfh", tfh);
                properties.Add("dkyt", dkyt);
                properties.Add("dkmj", dkmj);

                fea.GeoProperties=properties;

                //坐标串
                GeoJsonGeometry geo = new GeoJsonGeometry();
                geo.Type="Polygon";
                double[][][] coordinates = new double[ringCount][][];
                for (int j = 0; j < ringCount; j++)
                {
                    Ring ring = this.DKRings[j];

                    int pointCount = ring.JzdPoints.Count;
                    double[][] pointCoords = new double[pointCount][];
                    for (int k = 0; k < pointCount; k++)
                    {
                        JzdPoint pt = ring.JzdPoints[k];
                        double x = 0.0;
                        double y = 0.0;
                        Double.TryParse(String.Format("%.3f", pt.X),out x);
                        Double.TryParse(String.Format("%.3f", pt.Y),out y);

                        double[] coords = new double[2];
                        coords[0] = x;
                        coords[1] = y;

                        pointCoords[k] = coords;
                    }
                    coordinates[j] = pointCoords;
                }

                geo.Coordinates=coordinates;

                fea.Geometry=geo;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }

            return JsonConvert.SerializeObject(fea);
        }
    }
}
